package com.electric.controller;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.electric.common.CommonStringUtils;
import com.electric.common.Contains;
import com.electric.common.LoggerInterface;
import com.electric.entity.RepairType;
import com.electric.service.IRepairRecordService;
import com.electric.service.IRepairTypeService;

/**
 * <p>
 * 故障类型 前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Controller
@RequestMapping("/repairType")
public class RepairTypeController implements LoggerInterface{
	@Autowired
	private IRepairTypeService repairTypeService;
	@Autowired
	private IRepairRecordService repairRecordService;
	
	@RequestMapping("/init")
	public ModelAndView init() {
		ModelAndView view = list(1, 0, null);
		view.setViewName("/repairType/init");
		return view;
	}
	
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
			@RequestParam(defaultValue = "0") int pageSize, String keyword) {
		ModelAndView view = new ModelAndView("/repairType/contentTBody");
		
		if (pageSize == 0) {
			pageSize = Contains.PAGE_SIZE;
		}
		
		Page<RepairType> page = repairTypeService.selectPage(pageNum,pageSize,keyword);
		
		view.addObject("page",page);
		
		logger.debug(page);
		
		return view;
	}

	@RequestMapping("/moveList")
	public ModelAndView moveList(@RequestParam(defaultValue = "1", name = "pageNum") int pageNum,
			@RequestParam(defaultValue = "0") int pageSize,
			@RequestParam(name = "oldSeqId", required = true) String oldSeqId, String keyword) {
		if (pageSize == 0) {
			pageSize = Contains.MOVE_PAGE_SIZE;
		}

		ModelAndView view = list(pageNum, pageSize, keyword);

		view.setViewName("/repairType/moveListTable");

		view.addObject("oldSeqId", oldSeqId);
		view.addObject("keyword", keyword);

		return view;
	}
	
	@RequestMapping("/preview")
	public ModelAndView preview(String seqId) {
		ModelAndView view = new ModelAndView("/repairType/form");
		
		RepairType repairType = repairTypeService.selectById(seqId);
		
		view.addObject("repairType",repairType);
		
		return view;
	}
	
	@RequestMapping("/detail")
	public ModelAndView detail(String seqId) {
		ModelAndView view = new ModelAndView("/repairType/form");
		
		if(StringUtils.isNotBlank(seqId)) {
			RepairType repairType = repairTypeService.selectById(seqId);
			
			view.addObject("repairType",repairType);
		}
		
		return view;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public String insert(String repairType) {
		String uuid = CommonStringUtils.getUUID();

		RepairType dmlObj = new RepairType();
		dmlObj.setSeqId(uuid);
		dmlObj.setRepairType(repairType);
		
		String check = validate(dmlObj);
		if("success".equals(check)) {
			try {
				repairTypeService.insert(dmlObj);
			} catch(Exception e) {
				check = "DB_ER";
				logger.error(e.getMessage(),e);
			}
		}
		return check;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String modify(String seqId,String repairType) {
		RepairType dmlObj = new RepairType();
		dmlObj.setSeqId(seqId);
		dmlObj.setRepairType(repairType);
		
		String check = validate(dmlObj);
		if("success".equals(check)) {
			try {
				repairTypeService.updateById(dmlObj);
			} catch(Exception e) {
				check = "DB_ER";
				logger.error(e.getMessage(),e);
			}
		}
		return check;
	}
	
	@RequestMapping("/moveData")
	@ResponseBody
	public String moveData(String seqId, String newSeqId) {
		try {
			repairRecordService.moveDataByRepairType(seqId, newSeqId);
			return "success";
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return "DB_ER";
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(String seqId) {
		try {
			if(Contains.REPAIR_TYPE_ID_CODE.size() == 0){
				repairTypeService.loadRepairTypeToContainsMap();
			}
			
			if(repairRecordService.checkExistsByRepairType(seqId)) {
				return "RR_ES";
			}
			// 失压不允许删除，因为它有特殊操作
			else if(Contains.VOLTAGE_LOSS_CODE.equals(Contains.REPAIR_TYPE_ID_CODE.get(seqId))){
				return "VL_CD";
			}
			else {
				repairTypeService.deleteById(seqId);
				return "success";
			}
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
			return "DB_ER";
		}
	}
	
	@RequestMapping("/cleanData")
	@ResponseBody
	public String cleanData(String seqId) {
		try {
			repairTypeService.deleteById(seqId);
			repairRecordService.cleanDataByRepairType(seqId);
			return "success";
		} catch(Exception e) {
			logger.error(e.getMessage(),e);
			return "DB_ER";
		}
	}
	
	private String validate(RepairType repairType) {
		if(StringUtils.isBlank(repairType.getRepairType())) {
			return "RT_NP";
		}
		if(StringUtils.isBlank(repairType.getSeqId())) {
			return "ID_NP";
		}
		return "success";
	}
}

